import { BaseEntity, Column, Entity, Index, ManyToOne, PrimaryGeneratedColumn } from "typeorm";
import { DepositEther, TransactionStatus } from "./DepositEther";

@Entity()
export class TransferToken extends BaseEntity {
  @PrimaryGeneratedColumn({ type: 'bigint' })
  id: bigint;

  @Column({ nullable: true })
  txid: string;

  @Column()
  @Index()
  sender: string;

  @Column()
  privateKey: string;

  @Column()
  @Index()
  toAddr: string;

  @Column({ nullable: true })
  nonce: number;

  @Column({ nullable: true })
  gasPrice: string;

  @Column()
  @Index()
  status: TransactionStatus;

  @Column()
  token: string;

  @Column()
  amount: string;

  @Column({ nullable: true, type: "text" })
  note: string;

  @Column({ type: 'timestamp', default: () => "CURRENT_TIMESTAMP" })
  timestamp: string;

  @ManyToOne(() => DepositEther, d => d.transfers)
  deposit: DepositEther;
}

export class SerializedTransferToken {
  id: bigint;
  txid: string;
  sender: string;
  toAddr: string;
  nonce: number;
  gasPrice: string;
  status: TransactionStatus;
  token: string;
  amount: string;
  note: string;
  timestamp: string;
  deposit: bigint;

  static fromTransferToken(e: TransferToken) {
    const obj = new SerializedTransferToken();
    obj.id = e.id;
    obj.txid = e.txid;
    obj.sender = e.sender;
    obj.toAddr = e.toAddr;
    obj.nonce = e.nonce;
    obj.gasPrice = e.gasPrice;
    obj.status = e.status;
    obj.amount = e.amount;
    obj.token = e.token;
    obj.note = e.note;
    obj.timestamp = e.timestamp;
    obj.deposit = e.deposit.id;
    return obj;
  }
}
